package com.mylove.judas.yaya.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.formats.NativeAppInstallAd;
import com.mylove.judas.yaya.R;
import com.mylove.judas.yaya.entity.ImageEntity;

import java.util.List;

/**
 * Created by Judas on 2018/5/5.
 */

public class MeiTuListAdapter extends RecyclerView.Adapter {

    private static final int TYPE_IMAGE = 0;
    private static final int TYPE_AD = 1;

    private Context mContext;

    public MeiTuListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    private List<ImageEntity> list;

    public void setList(List<ImageEntity> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = null;
        switch (viewType){
            case TYPE_IMAGE:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_list_meitu, parent, false);
                return new MeiTuHolder(view);
            default:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list_ad, parent, false);
                break;
        }

        return new AdViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        ImageEntity item = list.get(position);
        if(holder instanceof MeiTuHolder){
            MeiTuHolder mth = (MeiTuHolder) holder;

            ViewGroup.LayoutParams params = mth.iv_meitu.getLayoutParams();
            params.height = params.width;

            mth.iv_meitu.setLayoutParams(params);

            mth.loadImage(item.getUrl());
        }else if(holder instanceof AdViewHolder){
            AdViewHolder avh = (AdViewHolder) holder;
            avh.loadAd();
        }

    }

    @Override
    public int getItemViewType(int position) {

        ImageEntity item = list.get(position);

        if(TextUtils.isEmpty(item.getUrl())){
            return TYPE_AD;
        }else{
            return TYPE_IMAGE;
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MeiTuHolder extends RecyclerView.ViewHolder{

        public ImageView iv_meitu;

        public MeiTuHolder(View itemView) {
            super(itemView);

            iv_meitu = (ImageView) itemView.findViewById(R.id.iv_meitu);

        }


        public void loadImage(String url){
            Glide.with(mContext).load(url)
//                        .override(width, height)
                    .placeholder(R.drawable.ic_image_default_white_24dp)
                    .error(R.drawable.ic_image_error_white_24dp)
                    .crossFade()
                    .centerCrop()
                    .into(iv_meitu);
        }

    }


    /**
     * adview
     */
    class AdViewHolder extends RecyclerView.ViewHolder{

        private AdView adview;

        public AdViewHolder(View itemView) {
            super(itemView);

            adview = (AdView) itemView.findViewById(R.id.adView);
        }

        public void loadAd(){

            adview.loadAd(new AdRequest.Builder()
//                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .build());

        }
    }

}
